<?php 
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class RuleController extends AdminBaseController{
	public function index(){
		$rule = M('Rule');
		$rlist = $rule -> field('id,title,pid,name,sort,status,addtime') ->  order('id asc') -> select();
		$rlist = _getTree($rlist,0,0,'&nbsp;|—');
		$this -> assign('rlist',$rlist);
		$this -> display();
	}

	public function add(){
		$pid = I('get.pid/d',0);//通过id添加
		$getRule = ['status' => 1,'pid' => $pid];
		$this -> assign('getRule',$getRule);
		$this -> display();
	}

	public function edit(){
		if(IS_GET){
			$id = I('get.id/d',0);
			if(!$id){
				$this -> ajaxReturn(['status' => 0,'mess' => '非法操作！']);
			}
			$getRule = M('Rule') -> field('id,pid,title,name,sort,status') ->where('id='.$id) ->find();
			$this -> assign('getRule',$getRule);
			$this -> display('Rule_add');
		}
	}

	public function save(){
		if(IS_AJAX || IS_POST){
			$id = I('post.id/d',0);
			if(empty($id)){
				$pid = I('post.pid/d',0);
			}else{
				$pid = I('post.epid/d',0);
			}
			$title = I('post.title/s','');
			if(!$title){
				$this -> ajaxReturn(['status' => 0, 'mess' => '权限名不能为空！']);
			}
			$name = I('post.name/s','');
			if(!$name){
				$this -> ajaxReturn(['status' => 0, 'mess' => '节点名称不能为空！']);
			}
			$sort = I('post.sort/d',0);
			$status = I('post.status/d');
			
			$rule = M('Rule');
			$ruleInfo = $rule->where(['title' => $title,'_logic' => 'OR','name' => $name])->count();
			if($ruleInfo > 0 && empty($id)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '该权限已存在！']);
			}
			$data = ['id' => $id,'pid' => $pid,'title' => $title,'name' => $name,'sort' => $sort,'status' => $status];
			$is_sql = $rule ->add($data,[],true);
			if(!$is_sql){
				$this -> ajaxReturn(['status' => 0, 'mess' => '失败了不要气馁！']);
			}
			$this -> ajaxReturn(['status' => 1, 'mess' => '成功了不要傲娇哦！']);
		}
	}

	public function del(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(!$id){
				$this -> ajaxReturn(['status' => 0,'mess' => '非法操作！']);
			}
			$is_count = M('Rule') -> where(['pid' => $id]) -> count();//删除之前先删除子集
			if($is_count > 0){
				$this -> ajaxReturn(['status' => 0, 'mess' => '请先删除子权限！']);
			}
			$is_del = M('Rule') -> where('id='.$id) -> delete();
			if(!$is_del){
				$this -> ajaxReturn(['status' => 0,'mess' => '删除失败！']);
			}
			$this -> ajaxReturn(['status' => 1,'mess' => '删除成功！']);

		}else{
			$this -> ajaxReturn(['status' => 0 , 'mess' => '非法操作！']);
		}
	}

	public function Alterstate(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(empty($id)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '非法操作！']);
			}
			$rule = M('Rule') -> field('status') -> where('id='.$id) -> find(); 
			if(0 == $rule['status']){
				M('Rule') -> where('id='.$id) -> setField(['status' => 1]); 
				$this -> ajaxReturn(['status' => 1, 'mess' => '已开启', 'data' => 1]);
			}else{
				M('Rule') -> where('id='.$id) -> setField(['status' => 0]);
				$this -> ajaxReturn(['status' => 1, 'mess' => '已禁用', 'data' => 0]);
			}
		}else{
			$this -> ajaxReturn(['status' => 0, 'mess' => '非法操作！']);
		}
	}


}


